<template>
    <div class="div-all">
        <div class="logo">
          <div class="logo-img" :style="{marginLeft:isCollapse?'10px':'5px'}">
          </div>
          <div class="logo-title-top" :style="{display:isCollapse?'none':'block'}">易构平台</div>
          <div class="logo-title-bottom" :style="{display:isCollapse?'none':'block'}">EasyBuilder</div>
        </div>

        <div class="div-menu" style=""> 
        <!-- 左侧菜单栏 -->
        <el-menu
          background-color="#333"
          text-color="#fff"
          class="menu"
          :collapse="isCollapse"
          :router="true"
           @select="select"
          :default-active="defaultActive"
        >
           <template v-for='m of menu'>
              <NavMenu  :key="m.id" :index="m.path" :menu='m'/>
          </template>
      </el-menu>
        </div>
    </div>
</template>



<script>
import Vue from 'vue';
import NavMenu from '../../components/NavMenu.vue';
import service from '../../config/utils/service.js';
import {Icon} from 'ant-design-vue';
let antd = {Icon};
Object.keys(antd).forEach(key => {
  Vue.use(key);
})
export default {
    name:'LeftMenu',
    data(){
        return {
            isCollapse: false,
            menu: [],
            defaultActive: '/start',
            btn_collapse_label: '收起<'
        }
    },
    methods: {
        //展开或收起左侧菜单栏
        collapse() {
            this.isCollapse = !this.isCollapse;
            if(this.isCollapse){
              this.btn_collapse_label = '展开>';
            } else {
              this.btn_collapse_label = '收起<';
            }
            document.getElementById("span_menuName").style.display=this.isCollapse?'none':'block'
        },
        //选中菜单
        select(index,indexPath) {

          console.log("index:" + index + " indexpath:" + indexPath);
          //拼接路径
          //let path = "/" + indexPath[0] + "/" + indexPath[1];
          //获取菜单名称
          let menuName = "";
          let path = "";
          this.menu.forEach(element => {
            if(element.path == index) {
                  menuName = element.menuName;
                  path = element.path;
                  return;
            }
            if(element.layer == 0 &&  element.children ){
              element.children.forEach(item => {
                if(item.path == index){
                  menuName = item.menuName;
                  path = element.path;
                  return;
                }
              });
            }
          });
          console.log("menuName: " +menuName);
          console.log("path: " +path);
          //调用父组件方法，创建tab和panel
          this.$parent.$parent.$parent.tabCreate(menuName, index, index);
        },
        //切换菜单(在主页切换tab页面时触发，父组件调子组件方法)
        changeMenu(index) {
          console.log("切换菜单,index:" + index);
          this.defaultActive = index;
          this.$router.replace(index);
        }
        
    },
    async beforeCreate(){
        console.log("beforeCreate........");
        var data = await service.get('/sys/menu/getAll');
        this.menu = data;
    },
    components: {
      NavMenu,
    }

}

    //请求菜单栏数据
</script>


<style scoped>
.div-all {
  position: relative;
  overflow: hidden;
}
.logo {
  width: 100%;
  height: 60px;
  line-height: 60px;
  position: relative;
}
.logo-img {
   width:40px;
   height:40px;
   float: left;
   background-image: url(../../assets/img/logo.png);
   background-size: 100%;
   margin: 5px 0 0 5px;
   border-radius: 5px;
}
.logo-title-top {
  position: absolute;
  margin-left: 60px;
  margin-top: -10px;
  font-size: 20px;
  font-weight: bolder;
  letter-spacing: 10px;
}
.logo-title-bottom {
  position: absolute;
  margin-left: 60px;
  margin-top: 10px;
  font-size: 14px;
  letter-spacing: 3px;
}
.div-menu {
  overflow: hidden;
} 

.menu {
  background-color: #555;
  text-align: left;
  margin-top: 0;
  overflow: hidden;
}
.menu:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.div-collapse {
  position: fixed;
  height:30px;
  line-height:30px;
  left: 0px;
  bottom: 0px;
}
::-webkit-scrollbar {
  width:5px;
}
</style>